package com.lifeonwalden.game.mindustry.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.lifeonwalden.game.mindustry.Global;
import com.lifeonwalden.game.mindustry.constant.ResourceType;
import com.lifeonwalden.game.mindustry.constant.SurfaceType;
import com.lifeonwalden.game.mindustry.entity.*;
import com.lifeonwalden.game.mindustry.model.RecipeItem;

public class OperationCenter implements Disposable {
    public static Surface toBuildUnit;
    public Stage stage;
    public Table centerContainer;
    public Table resourceCountingBoardContainer;
    public ResourceCountingBoard countingBoard;
    private Texture backgroundTexture;
    private Pixmap centerContainerBackgroundPixmap;
    private TextArea tooltip;

    public OperationCenter(TextureAtlas textureAtlas, BitmapFont font) {
        bindTextures(textureAtlas);
        this.stage = new Stage(new ScreenViewport());

        setupCenter(font);
    }

    private void setupCenter(BitmapFont font) {
        centerContainerBackgroundPixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
        centerContainerBackgroundPixmap.setColor(new Color(1, 1, 1, 0.6f));
        centerContainerBackgroundPixmap.fill();
        backgroundTexture = new Texture(centerContainerBackgroundPixmap);
        TextureRegionDrawable textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(backgroundTexture));

        centerContainer = new Table();
        centerContainer.setBackground(textureRegionDrawableBg);
        centerContainer.setSize(64 * 6f, 64 * 6f);
        centerContainer.top();
        centerContainer.left();

        ImageButton drillButton = new ImageButton(new TextureRegionDrawable(new Texture(Gdx.files.internal("sprites/blast-drill.png"))));
        drillButton.addListener(new InputListener() {
            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                StringBuilder tooltipText = new StringBuilder();
                if (null != SurfaceType.Drill.buildRecipeItems) {
                    for (RecipeItem recipeItem : SurfaceType.Drill.buildRecipeItems) {
                        tooltipText.append(recipeItem.resourceType.textureName).append(" : ").append(recipeItem.amount).append("\n");
                    }
                }
                tooltip.setText(tooltipText.toString());
                stage.addActor(tooltip);
            }

            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                tooltip.remove();
            }

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                if (Input.Buttons.LEFT == button) {
                    if (null != SurfaceType.Drill.buildRecipeItems) {
                        for (RecipeItem recipeItem : SurfaceType.Drill.buildRecipeItems) {
                            switch (recipeItem.resourceType) {
                                case Stone: {
                                    if (Global.world.resourceWarehouse.stoneCount < recipeItem.amount) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }

                    Drill drill = new Drill();
                    drill.type = SurfaceType.Drill;
                    toBuildUnit = drill;
                }
                return true;
            }
        });
        centerContainer.add(drillButton).size(48, 48).pad(2);

        ImageButton conveyorButton = new ImageButton(new TextureRegionDrawable(new Texture(Gdx.files.internal("sprites/conveyor.png"))));
        conveyorButton.addListener(new InputListener() {
            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                StringBuilder tooltipText = new StringBuilder();
                if (null != SurfaceType.Conveyor.buildRecipeItems) {
                    for (RecipeItem recipeItem : SurfaceType.Conveyor.buildRecipeItems) {
                        tooltipText.append(recipeItem.resourceType.textureName).append(" : ").append(recipeItem.amount).append("\n");
                    }
                }
                tooltip.setText(tooltipText.toString());
                stage.addActor(tooltip);
            }

            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                tooltip.remove();
            }

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                if (Input.Buttons.LEFT == button) {
                    if (null != SurfaceType.Conveyor.buildRecipeItems) {
                        for (RecipeItem recipeItem : SurfaceType.Conveyor.buildRecipeItems) {
                            switch (recipeItem.resourceType) {
                                case Stone: {
                                    if (Global.world.resourceWarehouse.stoneCount < recipeItem.amount) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                    Conveyor conveyor = new Conveyor();
                    conveyor.type = SurfaceType.Conveyor;
                    toBuildUnit = conveyor;
                }

                return true;
            }
        });
        centerContainer.add(conveyorButton).size(48, 48).pad(2);

        ImageButton turretButton = new ImageButton(new TextureRegionDrawable(new Texture(Gdx.files.internal("sprites/turrent.png"))));
        turretButton.addListener(new InputListener() {
            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                StringBuilder tooltipText = new StringBuilder();
                if (null != SurfaceType.Turret.buildRecipeItems) {
                    for (RecipeItem recipeItem : SurfaceType.Turret.buildRecipeItems) {
                        tooltipText.append(recipeItem.resourceType.textureName).append(" : ").append(recipeItem.amount).append("\n");
                    }
                }
                tooltip.setText(tooltipText.toString());
                stage.addActor(tooltip);
            }

            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                tooltip.remove();
            }

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                if (Input.Buttons.LEFT == button) {
                    if (null != SurfaceType.Turret.buildRecipeItems) {
                        for (RecipeItem recipeItem : SurfaceType.Turret.buildRecipeItems) {
                            switch (recipeItem.resourceType) {
                                case Stone: {
                                    if (Global.world.resourceWarehouse.stoneCount < recipeItem.amount) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                    Turret turret = new Turret();
                    turret.type = SurfaceType.Turret;
                    toBuildUnit = turret;
                }

                return true;
            }
        });
        centerContainer.add(turretButton).size(48, 48).pad(2);

        centerContainer.setPosition(this.stage.getViewport().getWorldWidth() - 64 * 6, 0);
        this.stage.addActor(centerContainer);

        countingBoard = new ResourceCountingBoard();
        resourceCountingBoardContainer = new Table();
        resourceCountingBoardContainer.center();
        centerContainer.top();
        resourceCountingBoardContainer.add(new Image(ResourceType.Stone.texture)).size(ResourceType.Stone.texture.getRegionWidth() / 2, ResourceType.Coal.texture.getRegionHeight() / 2);
        countingBoard.stoneCounting = new Label("0", new Label.LabelStyle(font, null));
        resourceCountingBoardContainer.add(countingBoard.stoneCounting).size(36, 24).padLeft(5);

        resourceCountingBoardContainer.setPosition(this.stage.getViewport().getWorldWidth() / 2 + 32, this.stage.getViewport().getWorldHeight() - 24);
        this.stage.addActor(resourceCountingBoardContainer);

        tooltip = new TextArea("hello tooltip!", new TextField.TextFieldStyle(font, Color.WHITE, null, null, null));
        tooltip.setPosition(this.stage.getViewport().getWorldWidth() - 64 * 6, 64 * 6);
        tooltip.sizeBy(100);
    }

    public void resize(int width, int height) {
        this.centerContainer.setPosition(width - 64 * 6, 0);
        this.stage.getViewport().update(width, height, true);
    }

    private void bindTextures(TextureAtlas textureAtlas) {
        SurfaceType.Drill.texture = textureAtlas.findRegion(SurfaceType.Drill.textureName);
        SurfaceType.Conveyor.texture = textureAtlas.findRegion(SurfaceType.Conveyor.textureName);
        SurfaceType.ConveyorMove.texture = textureAtlas.findRegion(SurfaceType.ConveyorMove.textureName);
        SurfaceType.Turret.texture = textureAtlas.findRegion(SurfaceType.Turret.textureName);
        ResourceType.Coal.texture = new TextureRegion(new Texture(Gdx.files.internal("sprites/iconCoal.png")));
        ResourceType.Iron.texture = textureAtlas.findRegion(ResourceType.Iron.textureName);
        ResourceType.Stone.texture = new TextureRegion(new Texture(Gdx.files.internal("sprites/iconStone.png")));
        ResourceType.Steel.texture = textureAtlas.findRegion(ResourceType.Steel.textureName);
    }

    @Override
    public void dispose() {
        this.stage.dispose();
        this.backgroundTexture.dispose();
        this.centerContainerBackgroundPixmap.dispose();
    }
}
